#include "usersform.h"
#include "ui_usersform.h"
#include <QPainter>
#include <QStyleOption>
#include <QTableView>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QInputDialog>

UsersForm::UsersForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UsersForm)
{
    ui->setupUi(this);
    initTable();
}

UsersForm::~UsersForm()
{
    delete ui;
}

void UsersForm::initTable()
{
    //
    // 创建一个 QStandardItemModel
    QStandardItemModel *model = new QStandardItemModel(0, 3, this);
    model->setHorizontalHeaderLabels(QStringList() << "名称" << "权限" << "全名");

    // 将模型设置给 QTableView
    ui->tableView->setModel(model);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setShowGrid(false);
    ui->tableView->verticalHeader()->setVisible(false);

    // 标题头铺满
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    QStringList names = {"张三", "李四", "王五"};
    QStringList permissions = {"管理员", "普通用户", "访客"};
    QStringList fullNames = {"张三丰", "李思思", "王五五"};

    for (int i = 0; i < names.size(); ++i) {
        QList<QStandardItem*> items;
        items.append(new QStandardItem(names[i]));
        items.append(new QStandardItem(permissions[i]));
        items.append(new QStandardItem(fullNames[i]));
        model->appendRow(items);
    }

    ui->tableView->viewport()->update();
}

void UsersForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void UsersForm::on_pushButton_add_clicked()
{
    QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
    if (model) {
        bool ok;
        QString name = QInputDialog::getText(this, "添加用户", "请输入名称:", QLineEdit::Normal, "", &ok);
        if (ok && !name.isEmpty()) {
            QString permission = QInputDialog::getText(this, "添加用户", "请输入权限:", QLineEdit::Normal, "", &ok);
            if (ok) {
                QString fullName = QInputDialog::getText(this, "添加用户", "请输入全名:", QLineEdit::Normal, "", &ok);
                if (ok) {
                    QList<QStandardItem*> items;
                    items.append(new QStandardItem(name));
                    items.append(new QStandardItem(permission));
                    items.append(new QStandardItem(fullName));
                    model->appendRow(items);
                }
            }
        }
    }
}

void UsersForm::on_pushButton_edit_clicked()
{
    QModelIndexList selectedRows = ui->tableView->selectionModel()->selectedRows();
    if (selectedRows.isEmpty()) {
        QMessageBox::information(this, "提示", "请选择要编辑的用户。");
        return;
    }

    QModelIndex index = selectedRows.first();
    QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
    if (model) {
        bool ok;
        QString name = QInputDialog::getText(this, "编辑用户", "请输入新名称:", QLineEdit::Normal, model->item(index.row(), 0)->text(), &ok);
        if (ok) {
            QString permission = QInputDialog::getText(this, "编辑用户", "请输入新权限:", QLineEdit::Normal, model->item(index.row(), 1)->text(), &ok);
            if (ok) {
                QString fullName = QInputDialog::getText(this, "编辑用户", "请输入新全名:", QLineEdit::Normal, model->item(index.row(), 2)->text(), &ok);
                if (ok) {
                    model->item(index.row(), 0)->setText(name);
                    model->item(index.row(), 1)->setText(permission);
                    model->item(index.row(), 2)->setText(fullName);
                }
            }
        }
    }
}

void UsersForm::on_pushButton_del_clicked()
{
    QModelIndexList selectedRows = ui->tableView->selectionModel()->selectedRows();
    if (selectedRows.isEmpty()) {
        QMessageBox::information(this, "提示", "请选择要删除的用户。");
        return;
    }

    QModelIndex index = selectedRows.first();
    QStandardItemModel *model = qobject_cast<QStandardItemModel*>(ui->tableView->model());
    if (model) {
        model->removeRow(index.row());
    }
}
